/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.milhouz.wormdroid.gl.model;

import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11Ext;

import simpull.Wheel;

import com.milhouz.wormdroid.gl.util.Grid;

/**
 * This is the OpenGL ES version of a sprite. It is more complicated than the CanvasSprite class because it can be used in more than one way. This class can draw using a grid of verts, a grid of verts
 * stored in VBO objects, or using the DrawTexture extension.
 */
public class WheelSpriteGL extends Wheel {
	private int id;
	// The OpenGL ES texture handle to draw.
	private int mTextureName;
	// The id of the original resource that mTextureName is based on.
	private int mResourceId;
	// If drawing with verts or VBO verts, the grid object defining those verts.
	public Grid mGrid;
	private static final float Z = 0;
	// Color data
	public boolean isPlainColor;
	private int red, green, blue, alpha;
	
	
	public WheelSpriteGL(int id, int x, int y, int radius, boolean isFixed, float mass, float elasticity, float friction, float traction, int resourceId) {
		super(x, y, radius, isFixed, mass, elasticity, friction, traction);
		this.id = id;
		mResourceId = resourceId;
		// Setup the grid.  This is just a quad.
    Grid grid = new Grid(2, 2);
    grid.set(0, 0,  0.0f, 0.0f, 0.0f, 0.0f , 1.0f);
    grid.set(1, 0, radius*2, 0.0f, 0.0f, 1.0f, 1.0f);
    grid.set(0, 1, 0.0f, radius*2, 0.0f, 0.0f, 0.0f);
    grid.set(1, 1, radius*2, radius*2, 0.0f, 1.0f, 0.0f );
    mGrid = grid;
	}
	
	public int getId() {
		return id;
	}

	public void setTextureName(int name) {
		mTextureName = name;
	}

	public int getTextureName() {
		return mTextureName;
	}

	public void setResourceId(int id) {
		mResourceId = id;
	}

	public int getResourceId() {
		return mResourceId;
	}

	public void draw(GL10 gl, int zx, int zy) {
		if (isPlainColor)
			gl.glColor4x(red, green, blue, alpha);
		else
			gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureName);
		
		if (mGrid == null) {
			// Draw using the DrawTexture extension.
			((GL11Ext) gl).glDrawTexfOES(getGLX()+zx, getGLY()-zy, Z, getRadius()*2, getRadius()*2);
		} else {
			// Draw using verts or VBO verts.
			gl.glPushMatrix();
			gl.glLoadIdentity();
			gl.glTranslatef(getGLX()+zx, getGLY()-zy, Z);

			mGrid.draw(gl, true);

			gl.glPopMatrix();
		}
	}
	
	public void setColor(int red,int green,int blue,int alpha) {
		this.red = red;
		this.blue = blue;
		this.green = green;
		this.alpha = alpha;
		isPlainColor = true;
	}
	
	private float getGLX() {
  	return getX() - getRadius();
  }
	
	private float getGLY() {
  	return getY() - getRadius();
  }
	
}
